package com.liit.controller.weixin.controller;

import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.liit.weixin.auth.utils.AuthUtil;
import cn.liit.weixin.core.basic.jsapi.JsapiTicketSupport;
import cn.liit.weixin.core.config.Config;
import cn.liit.weixin.jssdk.basic.JssdkConfig;

import com.liit.controller.index.BaseController;
import com.liit.entity.OrgInfoFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.exception.WeixinException;
import com.liit.mapper.ResourcesMapper;
import com.liit.util.Common;

/**
 * @author dingweibo
 * @Title: WeixinBaseController.java
 * @Description: 微信所有Controller超类
 * @date 2016年3月31日
 */
public class WeixinBaseController extends BaseController {

	@Inject
	private ResourcesMapper orgMapper;

	/**
	 * 获取当前用户openid
	 * 
	 * @return
	 */
	protected String getOpenid() {
		return getWeixinUserInfo().getStr("openid");
	}

	/**
	 * 获取当前微信用户信息
	 * 
	 * @return
	 */
	protected WeixinUserFormMap getWeixinUserInfo() {
		WeixinUserFormMap weixinUser = doAuth();
		if (weixinUser == null)
			throw new WeixinException("1001", "获取用户授权信息失败");
		return weixinUser;
	}

	/**
	 * 通过授权链接跳转请求时
	 * 
	 * @return
	 */
	private WeixinUserFormMap doAuth() {
		WeixinUserFormMap wxUser = Common.getWeixinUserInfo();
		if (wxUser != null) {
			return wxUser;
		}
		try {
			String code = getPara("code");
			String state = getPara("state");
			if (code == null || state == null) {
				return null;
			}
			Map<String, Object> authResult = AuthUtil.auth_accessToken_url(code);
			wxUser = orgMapper.findbyFrist("openid", authResult.get("openid").toString(), WeixinUserFormMap.class);
			if (wxUser != null) {
				SecurityUtils.getSubject().getSession().setAttribute("wxUserSession", wxUser);
			}

		} catch (Exception e) {
		}
		return wxUser;
	}

	/**
	 * 获取当前用户机构信息
	 * 
	 * @return
	 */
	protected OrgInfoFormMap getOrgInfo() {
		try {
			return orgMapper.findbyFrist("id", getWeixinUserInfo().get("orgInfoId").toString(), OrgInfoFormMap.class);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取请求对象
	 * 
	 * @return
	 */
	protected HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取响应对象
	 * 
	 * @return
	 */
	protected HttpServletResponse getHttpServletResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

	/**
	 * 获取上下文路径
	 * 
	 * @return
	 */
	protected String getContextPath() {
		return getHttpServletRequest().getContextPath();
	}

	/**
	 * 跳转到微信成功页面
	 * 
	 * @param icon
	 *            默认success [success,info,warn,waiting,safe_success,safe_warn]
	 * @param title
	 *            标题 默认"操作成功"
	 * @param desc
	 *            详细内容 ,为空时不显示
	 * @param okLink
	 *            确定按钮链接 , 默认javascript:history.go(-1);
	 * @param btnOk
	 *            按钮名,默认"确定"
	 * @param cancleLink
	 *            取消按钮链接 如果为空"取消"将按钮不显示
	 * @param btnCancle
	 *            取消按钮名 默认"取消"
	 * @param detailLink
	 *            查看详情链接 如果为空"查看详情"链接将不显示
	 * @param txtDetail
	 *            查看详情链接名
	 */
	protected String doMsg(String icon, String title, String desc, String okLink, String btnOk, String cancleLink, String btnCancle, String detailLink, String txtDetail,
			RedirectAttributes redirectAttributes) {
		redirectAttributes.addFlashAttribute("icon", icon);
		redirectAttributes.addFlashAttribute("title", title);
		redirectAttributes.addFlashAttribute("desc", desc);
		redirectAttributes.addFlashAttribute("okLink", okLink);
		redirectAttributes.addFlashAttribute("btnOk", btnOk);
		redirectAttributes.addFlashAttribute("cancleLink", cancleLink);
		redirectAttributes.addFlashAttribute("btnCancle", btnCancle);
		redirectAttributes.addFlashAttribute("detailLink", detailLink);
		redirectAttributes.addFlashAttribute("txtDetail", txtDetail);
		return "redirect:/weixin/weixinMsg.shtml";
	}

	/**
	 * 使用jssdk
	 * 
	 * @param url
	 *            相对URL或绝对URL
	 */
	protected void useJSSDK(String url) {
		HttpServletRequest request = getHttpServletRequest();
		try {
			if (!url.startsWith("http")) {
				url = Config.notifyurl + request.getContextPath() + url;
			}
			request.setAttribute("jssdk", JssdkConfig.config(url, JsapiTicketSupport.getJsapi_ticket()));
		} catch (Exception e) {
			throw new WeixinException("jssdk权限异常");
		}
	}
}
